/**
 * @(#)ApkSimulatorTimer.java 2017年03月25日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import org.jsbd.boss.common.BaseThread;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.domian.MonitorConfigBean;
import org.jsbd.boss.domian.MonitorParam;
import org.jsbd.boss.service.IMonitorParamTimingService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 监控参数定时器
 * 
 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
 * @version 1.0.0
 */
public class MonitorParamConfigTimer extends BaseThread {
	public static final Logger LOGGER = LoggerFactory.getLogger(MonitorParamConfigTimer.class);

	@Autowired
	private IMonitorParamTimingService monitorParamTimingService;
	@Autowired
	private RedisClient redisClient;

	@Override
	public void start() {
		LOGGER.info("monitor param update start");
		try {
			if (redisClient.evalSetNx(CacheKey.MONITOR_PARAM_DAYLIMIT_LOCK_KEY, 6000)) {
				List<MonitorParam> monitorParams = monitorParamTimingService.findDeltaData(50);
				List<MonitorConfigBean> monitorConfigBeans = new ArrayList<MonitorConfigBean>();
				
				for (MonitorParam mp : monitorParams) {
					MonitorConfigBean monitorConfigBean = new MonitorConfigBean();
					monitorConfigBean.setItemKey(mp.getParam());
					monitorConfigBean.setItemValue(mp.getValue());
					monitorConfigBeans.add(monitorConfigBean);
				}
				
				if (!monitorConfigBeans.isEmpty()) {
					monitorParamTimingService.updateBatch(monitorConfigBeans, monitorParams);
				}
			}
		} catch (Exception e) {
			LOGGER.error("更新日限异常", e);
		} finally {
			redisClient.remove(CacheKey.MONITOR_PARAM_DAYLIMIT_LOCK_KEY);
		}
	}
}
